package one.stand.controller;

import com.google.common.collect.Lists;
import com.jmp.api.admin.dto.*;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.feign.GoodsTypeFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.ConvertUtils;
import one.stand.mapper.GoodsApiAdminMapper;
import one.stand.model.GoodsType;
import one.stand.model.ResultModel;
import one.stand.service.GoodsTypeService;
import one.stand.util.OneListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(description = "产品分类管理")
@RestController
@RequestMapping("adminApi/goods/type")
@Slf4j
public class GoodsTypeController {
    @Autowired
    private GoodsTypeFeign goodsTypeFeign;
    @Autowired
    private GoodsTypeService goodsTypeService;
    @Autowired
    private GoodsApiAdminMapper goodsApiAdminMapper;

    @ApiOperation("列表")
    @PostMapping("list")
    @SecurityAuthCheck
    public ResultModel<GoodsTypeListResponse> list() {
        List<GoodsTypeListResponse.Type> list = goodsApiAdminMapper.selectGoodsType(GoodsTypeService.PLATFORM_COMPANY_ID);

        List<GoodsTypeListResponse.Type> pageInfo = OneListUtil.group(list,
                type -> type.getParentId() == 0,
                GoodsTypeListResponse.Type::getGoodstypeId,
                GoodsTypeListResponse.Type::getParentId,
                (type, child) -> {
                    type.setGoodsNum(type.getGoodsNum() + child.getGoodsNum());
                    if (OneListUtil.isNotEmpty(type.getChildren())) {
                        type.getChildren().add(child);
                    } else {
                        type.setChildren(Lists.newArrayList(child));
                    }
                }
        );
        GoodsTypeListResponse response = GoodsTypeListResponse.builder()
                .types(pageInfo)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation("保存")
    @PostMapping("add")
    @SecurityAuthCheck
    public ResultModel add(@RequestBody GoodsTypeAddRequest request) {

        goodsTypeService.save(GoodsTypeService.PLATFORM_COMPANY_ID, request.getName(), request.getParentId(), request.getGoodstypeId(), request.getIsShow(), request.getPhoto());

        return ResultModel.success();
    }

    @ApiOperation("删除")
    @PostMapping("delete")
    @SecurityAuthCheck
    public ResultModel delete(@RequestBody GoodsTypeDeleteRequest request) {

        goodsTypeFeign.delete(com.jmp.base.dto.GoodsTypeDeleteRequest.builder().goodsTypeId(request.getGoodstypeId()).build());

        return ResultModel.success();
    }

    @ApiOperation("上移/下移")
    @PostMapping("move")
    @SecurityAuthCheck
    public ResultModel move(@RequestBody GoodsTypeMoveRequest request) {

        goodsTypeService.updateSort(request.getGoodstypeId(), request.getType(), GoodsTypeService.PLATFORM_COMPANY_ID);

        return ResultModel.success();
    }

    @ApiOperation("下拉列表-一级分类")
    @PostMapping("parent")
    @SecurityAuthCheck
    public ResultModel<GoodsTypeParentResponse> parent() {
        List<GoodsType> list = goodsApiAdminMapper.selectParent(GoodsTypeService.PLATFORM_COMPANY_ID);

        List<GoodsTypeParentResponse.Type> types = ConvertUtils.list(list, type -> GoodsTypeParentResponse.Type.builder()
                .id(type.getGoodstypeId())
                .name(type.getGroupName())
                .build());

        GoodsTypeParentResponse response = GoodsTypeParentResponse.builder()
                .types(types)
                .build();
        return ResultModel.success(response);
    }
}
